﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Xml.Linq;
using System.Threading;

namespace Chat_WebServer.Controllers
{
    public class AcceptFriendController : ApiController
    {
        // List yeu cau ket ban cuar toan bo
        public static List<string> allFriends = new List<string>();

        // Lay danh sach yeu cau ket ban theo ten
        public List<string> GET(string user)
        {
            List<string> friends = new List<string>();
            for (int i = 0; i < allFriends.Count; i++)
            {
                string us = allFriends[i].Split('@')[0];
                if (us == user)
                {
                    friends.Add(allFriends[i].Split('@')[1]);
                    allFriends.RemoveAt(i);
                }
            }
            if(friends.Count > 0)
                return friends;
            return null;
        }

        // Gui yeu cau ket ban
        public string POST(string user, string fr)
        {
            using (var db = new AccountEntities())
            {
                bool isFriend = false;
                var temp = db.Friends.ToList().FirstOrDefault(f => f.UserName.Trim() == user);
                XElement xFriends = XElement.Parse(temp.Friends);
                foreach (XElement friend in xFriends.Elements())
                {
                    if (fr == friend.Value)
                    {
                        isFriend = true;
                        break;
                    }
                }
                if (db.Accounts.ToList().FindIndex(a => a.Username.Trim() == fr) < 0)
                    isFriend = true;
                foreach (string str in allFriends)
                    if (str == (fr + "@" + user))
                        isFriend = true;
                if (!isFriend)
                    allFriends.Add(fr + "@" + user);
            }
            return null;
        }

        // Chap nhan yeu cau ket ban hoac huy ket ban
        public string POST(string user, string friend, int type)
        {
            if (type == 1)
            {
                using (var db = new AccountEntities())
                {
                    var us = db.Friends.ToList().FirstOrDefault(f => f.UserName.Trim() == user);
                    XElement xusFriends = XElement.Parse(us.Friends);
                    xusFriends.Add(new XElement("friend", friend));
                    us.Friends = xusFriends.ToString();

                    var fr = db.Friends.ToList().FirstOrDefault(f => f.UserName.Trim() == friend);
                    XElement xfrFriends = XElement.Parse(fr.Friends);
                    xfrFriends.Add(new XElement("friend", user));
                    fr.Friends = xfrFriends.ToString();

                    db.SaveChanges();
                    return "success";
                }
            }
            else if (type == 0)
            {
                using (var db = new AccountEntities())
                {
                    var temp = db.Friends.ToList().FirstOrDefault(f => f.UserName.Trim() == user);
                    XElement friends = XElement.Parse(temp.Friends);
                    foreach (XElement f in friends.Elements())
                        if (f.Value == friend)
                        {
                            f.Remove();
                            break;
                        }
                    temp.Friends = friends.ToString();
                    db.SaveChanges();
                    return "success";
                }
            }
            return null;
        }
    }
}
